﻿using System.Linq;
using System.Collections.Generic;
using QMSDAL;

namespace QuaManSys
{
    public class Permission
    {
        public struct InfoRecord
        {
            public  string id;
            //public  string name;
            public  string status;
        };

        public struct OperRecord
        {
            public  string id;
            public  string name;
        };

        public static bool JudgePermission(CurUser user, string menuId, string operID)
        {
            string emId = user.employeeId;
            string dept = user.department;
            using (var entity = new QuaManSysEntities())
            {
                bool permissionOrNot = false;
                var queryPermission = from p in entity.SysMenuSysRoleSysOperation
                                      join q in entity.SysRoleSysPerson
                                      on p.SysRoleId equals q.SysRoleId
                                      where q.SysPersonId == emId && p.SysMenuId
                                        == menuId && p.SysOperationId == operID
                                      select p;
                var queryDeptPermission = from p in entity.SysMenuSysRoleSysOperation
                                      join q in entity.SysRoleSysDept
                                      on p.SysRoleId equals q.SysRoleId
                                      where q.Dept == dept && p.SysMenuId
                                        == menuId && p.SysOperationId == operID
                                          select p;
                if (queryPermission.Count() + queryDeptPermission.Count() != 0)
                    permissionOrNot = true;
                return permissionOrNot;
            }
            
        }

        /// <summary>
        /// 通过该用户emId查询对应menuId的操作权限，放在List中
        /// </summary>
        /// <param name="emId"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static List<string> QueryOpers(string emId, string menuId)
        {
            using (var entity = new QuaManSysEntities())
            {
                var queryOpers = (from q in entity.SysOperation
                                  join p in entity.SysMenuSysRoleSysOperation
                                  on q.Id equals p.SysOperationId
                                  join r in entity.SysRoleSysPerson
                                  on p.SysRoleId equals r.SysRoleId
                                  where r.SysPersonId == emId &&
                                  p.SysMenuId == menuId
                                  select q.Id).Distinct();
                List<string> lstOpers = queryOpers.ToList();

                return lstOpers;
            }
        }

        /// <summary>
        /// 通过该用户所在部门dept对应menuId的操作权限，放在List中
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static List<string> QueryDeptOpers(string dept, string menuId)
        {
            using (var entity = new QuaManSysEntities())
            {
                var queryOpers = (from q in entity.SysOperation
                                  join p in entity.SysMenuSysRoleSysOperation
                                  on q.Id equals p.SysOperationId
                                  join r in entity.SysRoleSysDept
                                  on p.SysRoleId equals r.SysRoleId
                                  where r.Dept == dept &&
                                  p.SysMenuId == menuId
                                  select q.Id).Distinct();
                List<string> lstOpers = queryOpers.ToList();

                return lstOpers;
            }
        }

        /// <summary>
        /// 查询处理状态为InfoStatus的内部信息表，放在list中
        /// </summary>
        /// <param name="InfoStatus"></param>
        /// <returns></returns>
        public static  List<string> QueryInnerInfoStatus(string InfoStatus)
        {
            using (var entity = new QuaManSysEntities())
            {
                var inInfo = from q in entity.InnerQualityInfo
                             where q.processingStatus == InfoStatus
                             select q.qualityInfoId;
                List<string> lstInfo = inInfo.ToList();
                return lstInfo;
            }
        }

        /// <summary>
        /// 查询处理状态为InfoStatus的外部信息表，放在list中
        /// </summary>
        /// <param name="InfoStatus"></param>
        /// <returns></returns>
        public static List<string> QueryOuterInfoStatus(string InfoStatus)
        {
            using (var entity = new QuaManSysEntities())
            {
                var outInfo = from q in entity.OuterQualityInfo
                             where q.processingStatus == InfoStatus
                             select q.qualityInfoId;
                List<string> lstInfo = outInfo.ToList();
                return lstInfo;
            }
        }

        /// <summary>
        /// 查询处理状态为InfoStatus的顾客满意度信息表，放在list中
        /// </summary>
        /// <param name="InfoStatus"></param>
        /// <returns></returns>
        public static List<string> QueryCusInfoStatus(string InfoStatus)
        {
            using (var entity = new QuaManSysEntities())
            {
                var cusInfo = from q in entity.CusSatSur
                              where q.processStatus == InfoStatus
                              select q.infomationId;
                List<string> lstInfo = cusInfo.ToList();
                return lstInfo;
            }
        }
    }
}